
/*
 页面父类基础界面
 */
import 'package:xsdk_flutter/xsdk/framework/base/xsdk_function.dart';
import 'package:xsdk_flutter/xsdk/framework/bean/x_bean_menu.dart';
import 'package:xsdk_flutter/xsdk/framework/page/x_page_state.dart';
import 'package:flutter/material.dart';
import 'package:xsdk_flutter/xsdk/utils/x_utils_color.dart';
import 'package:xsdk_flutter/xsdk/utils/x_utils_image.dart';
import 'package:xsdk_flutter/xsdk/utils/x_utils_log.dart';
import 'package:xsdk_flutter/xsdk/utils/x_utils_style.dart';
import 'package:xsdk_flutter/xsdk/utils/x_utils_widget.dart';

/*
自定义底部导航栏,
自主控制其他不在底部栏中的page显示,自由改变底部导航栏数量;
 */
abstract class XPageBottomNavigationBarCustomState extends XPageState{

  bool isUseIndexedStack;
  bool isChangeBottomNavigationBarItemReLoad;

  XPageBottomNavigationBarCustomState({
    super.isKeepPageState=false,
    super.xTag,
    this.isChangeBottomNavigationBarItemReLoad=false,//true切换底部菜单按钮重新创建子Wiget
    this.isUseIndexedStack=false, //当changeBottomNavigationBarItemReLoad=false时(即切换不重新创建子Wiget), 如果子Widget存在webview,则需要启用true,否则每次切换子Widget(webview)都会重新加载
  });


  /*
  注意: 如果是XPage或XStatefulWidget,要传入key:UniqueKey(),否则不是唯一,切换bottomNavigationItemName时不会加载导致bug

  各个子page实测在子state中设置wantKeepAlive为true无法保存状态值,
  解决办法:
  每个子page都创建实例变量,每次重新调用都是同一个,然后把状态状态值保存到XPage中(stateValue1~9),
   */

  @required
  Widget getBottomNavigationItemWidget(String bottomNavigationItemName);

  final Map<String,dynamic> _pageMap={};
  final List<Widget> _pageList=[];
  final List<String> _pageNameList=[];
  Widget _getXBottomNavigationItemPage(String bottomNavigationItemName){

    //每次点击切换itemPage都会重新创建更新数据
    if(isChangeBottomNavigationBarItemReLoad){
      return getBottomNavigationItemWidget(bottomNavigationItemName);
    }else{
      //每次点击切换itemPage都保留上一次状态(不创建新的itemPage)
      if(isUseIndexedStack){
        //适用子widget存在webview场景
        return XWidgetUtils.getLayoutIndexedStack(index: _bottomNavigationItemIndex, children: _pageList);
      }else{
        if(_pageMap[bottomNavigationItemName]==null){
          _pageMap[bottomNavigationItemName]=getBottomNavigationItemWidget(bottomNavigationItemName);
        }
        return _pageMap[bottomNavigationItemName];
      }

    }

  }


  @required
  void onBottomNavigationItemOtherControlClick(String bottomNavigationItemName);

  //初始化或更新底部导航栏
  void updateXCustomBottomNavigationBar(
      {
        Color? backgroundColor,
        List<XBeanMenuItem>? items,
        bool isDefaultSelectFirstItem=false,//是否默认选中第一个
      }
      ){

    setState(() {

      _pageMap.clear();
      _pageList.clear();
      _pageNameList.clear();

      _customBottomNavigationBarBackgroundColor=backgroundColor;
      _customBottomNavigationBarItems.clear();

      if(items!=null){
        _customBottomNavigationBarItems.addAll(items);

        //默认第一个选中
        if(isDefaultSelectFirstItem){
          if(_customBottomNavigationBarItems.length!=0){
            _customBottomNavigationBarItems[0].isUserSelect=true;
          }
        }

        for(int i=0;i<_customBottomNavigationBarItems.length;i++){
          XBeanMenuItem iitem=_customBottomNavigationBarItems[i];
          if(iitem.isUserSelect){
            _bottomNavigationItemIndex=i;
            _bottomNavigationItemName=iitem.text??'';
            break;
          }
        }

        //
        if(isUseIndexedStack){
          //
          _pageNameList.addAll(_customBottomNavigationBarItems.map((XBeanMenuItem menuItem){
            return menuItem.text??'';
          }).toList());
          //
          _pageList.addAll(_customBottomNavigationBarItems.map((XBeanMenuItem menuItem){
            return getBottomNavigationItemWidget(menuItem.text??'');
          }).toList());

        }

      }
    });


  }


  //更新切换bottomNavigationBarItemPage
  void changeCustomBottomNavigationItemPage(
      String bottomNavigationItemName,
  {
    XFunctionDo? doEnd,
  }
      ){
    setState(() {

      _bottomNavigationItemName=bottomNavigationItemName;
      //init
      _bottomNavigationItemIndex=-1;
      //
      for(int i=0;i<_customBottomNavigationBarItems.length;i++){
        XBeanMenuItem iitem=_customBottomNavigationBarItems[i];
        if(bottomNavigationItemName==iitem.text){
          _bottomNavigationItemIndex=i;
          iitem.isUserSelect=true;
        }else{
          iitem.isUserSelect=false;
        }
      }

      //
      if(_bottomNavigationItemIndex==-1
          && !isChangeBottomNavigationBarItemReLoad
          && isUseIndexedStack
      ){
        for(int j=0;j<_pageNameList.length;j++){
          if(_pageNameList[j]==bottomNavigationItemName){
            //
            _bottomNavigationItemIndex=j;
            break;
          }
        }

        //
        if(_bottomNavigationItemIndex==-1){
          _pageNameList.add(bottomNavigationItemName);
          _pageList.add(getBottomNavigationItemWidget(bottomNavigationItemName));
          _bottomNavigationItemIndex=_pageNameList.length-1;
        }
      }

      if(doEnd!=null){
        doEnd();
      }

    });
  }


  ////////////////////////////////////////////////////////////////////////

  @override
  Widget getBodyWidget(BuildContext context) {

    if(_customBottomNavigationBarItems.isEmpty){
      return XWidgetUtils.getLayoutContainer();
    }

    return XWidgetUtils.getLayoutColumn(children: [
      //各个子page
      XWidgetUtils.getLayoutExpanded(_getXBottomNavigationItemPage(_bottomNavigationItemName)),
      //自定义底部导航栏
      XWidgetUtils.getLayoutContainer(
        height: XWidgetUtils.getBottomBarHeight(), //适配iOS底部标识条80, android为75
        alignment: Alignment.topCenter,
        padding: XWidgetUtils.getEdgeInsets(0, 5, 0, 0),
        decoration:XWidgetUtils.getBoxDecoration(
          border: Border(top: BorderSide(color: XColorUtils.dividerColor)),
          backgroundColor: _customBottomNavigationBarBackgroundColor?? const Color(0xFFFFFFFF),
        ),
        child: XWidgetUtils.getLayoutContainer(
          height: 48,
          child: XWidgetUtils.getLayoutRow(
              crossAxisAlignment:CrossAxisAlignment.start,
              children:_customBottomNavigationBarItems.map(
                      (XBeanMenuItem item){
                    return XWidgetUtils.getLayoutExpanded(
                        XWidgetUtils.getWidgetGestureDetector(
                            child: XWidgetUtils.getLayoutContainer(
                              alignment: Alignment.center,
                              backgroundColor: _customBottomNavigationBarBackgroundColor?? const Color(0xFFFFFFFF),
                              child: XWidgetUtils.getLayoutColumn(
                                  mainAxisSize: MainAxisSize.min,
                                  mainAxisAlignment:MainAxisAlignment.center,
                                  crossAxisAlignment:CrossAxisAlignment.center,
                                  children: [
                                    XImageUtils.getImageWidgetWithAsset(
                                      item.selectImage==null?item.image!:item.isUserSelect?item.selectImage!:item.image!,
                                      color: item.isUserSelect?XColorUtils.appMainColor:XColorUtils.color999999,
                                      width: item.imageWidth,
                                      height: item.imageHeight,
                                    ),
                                    XWidgetUtils.getWidgetText(
                                        item.text!,
                                        padding: XWidgetUtils.getEdgeInsets(0, 3, 0, 0),
                                        style: XStyleUtils.getTextStyle(
                                          fontSize: 10,
                                          color: item.isUserSelect?XColorUtils.appMainColor:XColorUtils.color666666,
                                        )
                                    )
                                  ]
                              ),
                            ),
                            onTap: (){
                              if(item.isOtherControlOnClick){
                                //不是bottomNavigationBarItemPage

                                setState(() {
                                  for(XBeanMenuItem iitem in _customBottomNavigationBarItems){
                                    if(item.text==iitem.text){
                                      iitem.isUserSelect=true;
                                    }else{
                                      iitem.isUserSelect=false;
                                    }
                                  }
                                });

                                onBottomNavigationItemOtherControlClick(item.text!);
                              }else{
                                //是bottomNavigationBarItemPage
                                changeCustomBottomNavigationItemPage(item.text!);
                              }
                            }
                        )
                    );
                  }).toList()
          ),
        ),
      )
    ]);
  }



  Color? _customBottomNavigationBarBackgroundColor;
  final List<XBeanMenuItem> _customBottomNavigationBarItems=[];

  String _bottomNavigationItemName='';
  int _bottomNavigationItemIndex=0;


  @override
  void onDestroy() {
    super.onDestroy();
    _pageMap.clear();
  }

}